/**
 * =================================================================
 * 版权所有 2011-2013 深圳市泰海网络科技服务有限公司，并保留所有权利
 * -----------------------------------------------------------------
 * 这不是一个自由软件！您不能在任何未经允许的前提下对程序代码进行修改和使用；
 * 不允许对程序代码以任何形式任何目的的再发布
 * =================================================================
 */
package com.imoney.payment.trade.generator;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.imoney.framework.base.entity.IRequest;
import com.imoney.framework.base.enums.CcyType;
import com.imoney.framework.base.exception.ServiceException;
import com.imoney.framework.base.utils.StringUtils;
import com.imoney.payment.generator.AbstractGenerator;
import com.imoney.payment.trade.dao.IDataCommDao;
import com.imoney.payment.trade.domain.EmailItem;
import com.imoney.payment.trade.domain.TradeOrder;
import com.imoney.payment.trade.domain.TransferRequest;
import com.imoney.payment.trade.enums.BusinessType;
import com.imoney.payment.trade.enums.TradeStatus;

/**
 * 类说明：<br>
 * 
 * <p>
 * 详细描述：<br>
 * 
 * </p>
 * 
 * <pre>
 * ——————————————————————————————————————————————————————————————————
 * |		修改人		|		修改时间			|		修改原因
 * ——————————————————————————————————————————————————————————————————
 * |	zengxx 曾宪新	|		2013-6-16		|	
 * ——————————————————————————————————————————————————————————————————
 * </pre>
 * 
 * @author zengxx 曾宪新(Xavier.zeng)
 * 
 *         CreateDate: 2013-6-16
 */
@Service
public class TradeOrderGenerator extends AbstractGenerator<TradeOrder> {

	@Resource
	private IDataCommDao commonDao;

	@Override
	public TradeOrder generate(IRequest request) throws ServiceException {

		return null;
	}

	@Override
	public List<TradeOrder> generateList(IRequest request)
			throws ServiceException {
		TransferRequest trRequest = (TransferRequest) request;
		List<EmailItem> emailItems = trRequest.getEmailItems();

		List<TradeOrder> tradeList = new ArrayList<TradeOrder>();
		TradeOrder tradeOrder = null;
		for (EmailItem item : emailItems) {
			tradeOrder = new TradeOrder();
			tradeOrder.setTradeNo(commonDao.getBizNo(1,
					trRequest.getBusinessType()));
			item.setTradeNo(tradeOrder.getTradeNo());
			if (StringUtils.isNotEmpty(trRequest.getOutBizNo())) {
				tradeOrder.setOutBizNo(trRequest.getOutBizNo());
			}
			tradeOrder.setTradeType(trRequest.getTradeType());
			tradeOrder.setProductType(trRequest.getProductType());
			tradeOrder.setBusinessType(trRequest.getBusinessType());
			tradeOrder.setCcyType(CcyType.CNY);
			tradeOrder.setSubject(trRequest.getSubject());
			tradeOrder.setMemo(trRequest.getMemo());
			tradeOrder.setRemark(trRequest.getRemark());
			if (BusinessType.GATHER.equals(trRequest.getBusinessType())) {
				tradeOrder.setSellerEmail(trRequest.getFromEmail());
				tradeOrder.setBuyerEmail(item.getToEmails());
			}
			if (BusinessType.PAYMENT.equals(trRequest.getBusinessType())
					|| BusinessType.TRANSFER
							.equals(trRequest.getBusinessType())) {
				tradeOrder.setSellerEmail(item.getToEmails());
				tradeOrder.setBuyerEmail(trRequest.getFromEmail());
			}
			tradeOrder.setQuantity(item.getQuantity());
			tradeOrder.setAmt(item.getAmt());
			tradeOrder.setStatus(TradeStatus.WAITPAY);

			tradeList.add(tradeOrder);
		}

		return tradeList;
	}

}
